import javax.swing.*;
import javax.swing.tree.*;
import java.awt.BorderLayout;

import javax.swing.event.*;

public class tree extends JComponent implements TreeSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DefaultMutableTreeNode rootDb;
	DefaultMutableTreeNode childDb;
	DefaultMutableTreeNode dbTableName;
	DefaultMutableTreeNode dbTableName2;	
	public tree(final Integer index) {
		
		//create layout for the tree
		setLayout(new BorderLayout());
		
		//add some demo element to the tree
		rootDb = new DefaultMutableTreeNode("Databases");
		childDb = new DefaultMutableTreeNode("root");
		rootDb.add(childDb);
		dbTableName = new DefaultMutableTreeNode("child");
		childDb.add(dbTableName);
		dbTableName2 = new DefaultMutableTreeNode("child2");
		childDb.add(dbTableName2);
		
		
		//create a JTree component from the entire rootDb tree
		JTree tree = new JTree(rootDb);
		tree.setVisible(true);
		//add it to the JScrollPane
		JScrollPane jsp = new JScrollPane(tree);
		add(jsp, BorderLayout.CENTER);
		
		// Add our selection listener, it'll expand for us the databases and tables
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent tse) {
				TreePath tp = tse.getNewLeadSelectionPath();
				if (tse.getNewLeadSelectionPath() != null) {
					String tableName = null;
					if (tp.getPathCount() == 3) {
						tableName = tp.getLastPathComponent().toString();
						
						//get the last item from the tree and print it to stdout
						if(tableName == "child") {
						table.fillTableCols(4, index);
						} else if(tableName == "child2") {
							table.fillTableCols(1, index);
						} else {
							System.out.println("Ooops..");
						}
					}
					}
					else {
					}
				}
		});
	}

	public void valueChanged(TreeSelectionEvent event) {
	}
}